package gam.n1.conf;

import com.google.common.collect.Maps;
import gam.n1.areas.impl.a.config.DefendVConfig;
import gam.n1.conf.i.AreaConfig;
import gam.n1.conf.i.MsgConfig;
import org.bukkit.plugin.Plugin;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

public final class ConfManager {
    private final Plugin plugin;
    private static Map<String,AConfig> configs ;
    public static MsgConfig Msg;
    public static AreaConfig DVArea;
    public ConfManager(Plugin plugin) {
        this.plugin = plugin;
        registerConfig("msg", new MsgConfig(plugin,"msg"));
        registerConfig("DVarea", new DefendVConfig(plugin,"area"));
        Msg = (MsgConfig) getConfig("msg");
        DVArea = (AreaConfig) getConfig("DVarea");
    }

    public static AConfig getConfig(String key){
        return configs.getOrDefault(key, null);
    }
    public static void registerConfig(String key,AConfig config){
        Objects.requireNonNull(key);
        Objects.requireNonNull(config);
        if (configs == null){
            configs = Maps.newHashMap();
        }
        configs.put(key,config);
    }
    public static boolean reload(Plugin plugin){
        return configs.values().stream().allMatch(config -> {
            try {
                return config.reload(plugin);
            }  catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        });
    }
    public void load(Plugin plugin){
        configs.forEach((n,a) -> {
            try {
                a.load(plugin);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
